#!/bin/bash
#***************************************************
#
#Author: natpacket
#date: 2021/6/7
#Description: 自动部署web服务
#
#***************************************************
branch="asen"
execu_name="libai_backen.jar"
jar_pid=`ps -aux | grep ${execu_name} | grep -v grep | tr -s ' ' | cut -d ' ' -f 2`
repo_path="/home/ubuntu/git_repo/libai_backend.git/Committee_Backend"
target_path="/home/ubuntu/git_repo/libai_backend.git/Committee_Backend/target/*.jar"
run_path="/home/ubuntu/libai/backend/$execu_name"
cd $repo_path
unset GIT_DIR
# git pull 
echo "**********正在更新最新代码**********"
git pull origin $branch:$branch
git checkout $branch
# package
echo "**********正在打包！************"
mvn clean package > package.log 2>&1
echo "**********打包完成！************"
# mv
if [ ! -f $target_path ];then
		echo "jar package 不存在,请尝试手动打包！"
	else
			mv $target_path $run_path
fi
# kill
if [ ! -n "$jar_pid" ];then
		echo "没有正在运行的任务"
	else
			kill -9 $jar_pid
fi
# run
if [ ! -f "$run_path" ];then
		echo "目标文件不存在，无法运行！请检查！"
	else
			nohup java -jar $run_path > nohup.out 2>&1 &
fi
if [ $? -eq 0 ];then
	echo "*******************任务运行成功******************"
	ps -aux | grep $execu_name | grep -v 'grep'
else
	echo "######################任务运行失败，请检查目标文件是否存在！###########################"
fi
